package com.huaboot.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaboot.commont.page.PageResult;
import com.huaboot.system.entity.SysUserPostEntity;
import com.huaboot.system.entity.SysUserRoleEntity;
import com.huaboot.system.mapper.SysUserPostMapper;
import com.huaboot.system.mapper.SysUserRoleMapper;
import com.huaboot.system.query.SysPostQuery;
import com.huaboot.system.service.SysPostService;
import com.huaboot.system.service.SysUserPostService;
import com.huaboot.system.service.SysUserRoleService;
import com.huaboot.system.vo.SysPostVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * description: SysUserPostServiceImpl
 * date: 2023/9/7 17:41
 * author: ff
 */
@Service
public class SysUserPostServiceImpl  extends ServiceImpl<SysUserPostMapper, SysUserPostEntity> implements SysUserPostService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long userId, List<Long> postIdList) {
        if (userId == null  ||  CollectionUtil.isEmpty(postIdList)){
            return;
        }
        remove(Wrappers.<SysUserPostEntity>lambdaQuery().eq(SysUserPostEntity::getUserId, userId));
        List<SysUserPostEntity> collect = postIdList.stream().map(item -> {
            SysUserPostEntity sysUserPost = new SysUserPostEntity();
            sysUserPost.setPostId(item);
            sysUserPost.setUserId(userId);
            return sysUserPost;
        }).collect(Collectors.toList());
        saveBatch(collect);
    }

    @Override
    public void deleteByPostIdList(List<Long> postIdList) {
        remove(new LambdaQueryWrapper<SysUserPostEntity>().in(SysUserPostEntity::getPostId, postIdList));

    }

    @Override
    public void deleteByUserIdList(List<Long> userIdList) {
        baseMapper.delete(new LambdaQueryWrapper<SysUserPostEntity>().in(SysUserPostEntity::getUserId, userIdList));
    }

    @Override
    public List<Long> getPostIdList(Long userId) {
        return baseMapper.getPostIdList(userId);
    }
}
